home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / c / gcc222-3.lha / const_class / list.hp < prev    next >
Encoding:
Text File  |  1993-02-14  |  5.9 KB  |  273 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>List_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>List_h 1
  25.  
  26. #ifndef _<T>_typedefs
  27. #define _<T>_typedefs 1
  28. typedef void (*<T>Procedure)(<T&>);
  29. typedef <T>  (*<T>Mapper)(<T&>);
  30. typedef <T>  (*<T>Combiner)(<T&>, <T&>);
  31. typedef int  (*<T>Predicate)(<T&>);
  32. typedef int  (*<T>Comparator)(<T&>, <T&>);
  33. #endif
  34.  
  35. #include <Pix.h>
  36. #include "<T>.defs.h"
  37.  
  38. struct <T>ListNode
  39. {
  40.   <T>ListNode*          tl;
  41.   short                 ref;
  42.   <T>                   hd;
  43. };
  44.  
  45. extern <T>ListNode Nil<T>ListNode;
  46.  
  47. class <T>List
  48. {
  49. protected:
  50.   <T>ListNode*          P;
  51.  
  52.                         <T>List(<T>ListNode* p);
  53. public:
  54.                         <T>List();
  55.                         <T>List(<T&> head);
  56.                         <T>List(<T&> head, <T>List& tl);
  57.                         <T>List(<T>List& a);
  58.                         <T>List(Pix p);
  59.                         ~<T>List();
  60.  
  61.   <T>List&              operator = (<T>List& a);
  62.  
  63.   int                   null();
  64.   int                   valid();
  65.                         operator const void* ();
  66.   int                   operator ! ();
  67.  
  68.   int                   length();
  69.   int                   list_length();
  70.  
  71.   <T>&                  get();
  72.   <T>&                  head();
  73.   <T>&                  operator [] (int n);
  74.  
  75.   <T>List               nth(int n);
  76.   <T>List               tail();
  77.   <T>List               last();
  78.  
  79.   <T>List               find(<T&> targ);
  80.   <T>List               find(<T>List& targ);
  81.   int                   contains(<T&> targ);
  82.   int                   contains(<T>List& targ);
  83.   int                   position(<T&> targ);
  84.  
  85.   friend <T>List        copy(<T>List& a);
  86.   friend <T>List        concat(<T>List& a, <T>List& b);
  87.   friend <T>List        append(<T>List& a, <T>List& b);
  88.   friend <T>List        map(<T>Mapper f, <T>List& a);
  89.   friend <T>List        merge(<T>List& a, <T>List& b, <T>Comparator f);
  90.   friend <T>List        combine(<T>Combiner f, <T>List& a, <T>List& b);
  91.   friend <T>List        reverse(<T>List& a);
  92.   friend <T>List        select(<T>Predicate f, <T>List& a);        
  93.   friend <T>List        remove(<T&> targ, <T>List& a);
  94.   friend <T>List        remove(<T>Predicate f, <T>List& a);
  95.   friend <T>List        subst(<T&> old, <T&> repl, <T>List& a);
  96.  
  97.   void                  push(<T&> x);
  98.   <T>                   pop();
  99.  
  100.   void                  set_tail(<T>List& p);
  101.   void                  append(<T>List& p);
  102.   void                  prepend(<T>List& p);
  103.   void                  del(<T&> targ);
  104.   void                  del(<T>Predicate f);
  105.   void                  select(<T>Predicate f);
  106.   void                  subst(<T&> old, <T&> repl);
  107.   void                  reverse();
  108.   void                  sort(<T>Comparator f);
  109.  
  110.   void                  apply(<T>Procedure f);
  111.   <T>                   reduce(<T>Combiner f, <T&> base);
  112.  
  113.   friend int            operator == (<T>List& a, <T>List& b);
  114.   friend int            operator != (<T>List& a, <T>List& b);
  115.  
  116.   Pix                   first();
  117.   void                  next(Pix& p);
  118.   Pix                   seek(<T&> item);
  119.   <T>&                  operator () (Pix p);
  120.   int                   owns(Pix p);
  121.  
  122.   void                  error(const char*);
  123.   int                   OK();
  124. };
  125.  
  126.  
  127. inline void reference(<T>ListNode* p)
  128. {
  129.   if (p->ref >= 0) ++p->ref;
  130. }
  131.  
  132. inline void dereference(<T>ListNode* p)
  133. {
  134.   while (p->ref > 0 && --p->ref == 0)
  135.   {
  136.     <T>ListNode* n = p->tl;
  137.     delete(p);
  138.     p = n;
  139.   }
  140. }
  141.  
  142.  
  143. inline <T>ListNode* new<T>ListNode(<T&> h)
  144. {
  145.   <T>ListNode* p = new <T>ListNode;
  146.   p->ref = 1;
  147.   p->hd = h;
  148.   return p;
  149. }
  150.  
  151. inline <T>ListNode* new<T>ListNode(<T&> h, <T>ListNode* t)
  152. {
  153.   <T>ListNode* p = new <T>ListNode;
  154.   p->ref = 1;
  155.   p->hd = h;
  156.   p->tl = t;
  157.   return p;
  158. }
  159.  
  160.  
  161. inline <T>List::~<T>List()
  162. {
  163.   dereference(P);
  164. }
  165.  
  166. inline <T>List::<T>List()
  167. {
  168.   P = &Nil<T>ListNode;
  169. }
  170.  
  171. inline <T>List::<T>List(<T>ListNode* p)
  172. {
  173.   P = p;
  174. }
  175.  
  176. inline <T>List::<T>List(<T&> head)
  177. {
  178.   P = new<T>ListNode(head);
  179.   P->tl = &Nil<T>ListNode;
  180. }
  181.  
  182. inline <T>List::<T>List(<T&> head, <T>List& tl)
  183. {
  184.   P = new<T>ListNode(head, tl.P);
  185.   reference(P->tl);
  186. }
  187.  
  188. inline <T>List::<T>List(<T>List& a)
  189. {
  190.   reference(a.P);
  191.   P = a.P;
  192. }
  193.  
  194.  
  195. inline <T>& <T>List::get()
  196. {
  197.   return P->hd;
  198. }
  199.  
  200. inline <T>& <T>List::head()
  201. {
  202.   return P->hd;
  203. }
  204.  
  205.  
  206. inline <T>List <T>List::tail()
  207. {
  208.   reference(P->tl);
  209.   return <T>List(P->tl);
  210. }
  211.  
  212.  
  213.  
  214. inline int <T>List::null()
  215. {
  216.   return P == &Nil<T>ListNode;
  217. }
  218.  
  219. inline int <T>List::valid()
  220. {
  221.   return P != &Nil<T>ListNode;
  222. }
  223.  
  224. inline <T>List::operator const void* ()
  225. {
  226.   return (P == &Nil<T>ListNode)? 0 : this;
  227. }
  228.  
  229. inline int <T>List::operator ! ()
  230. {
  231.   return (P == &Nil<T>ListNode);
  232. }
  233.  
  234.  
  235. inline void <T>List::push(<T&> head)
  236. {
  237.   <T>ListNode* oldp = P;
  238.   P = new<T>ListNode(head, oldp);
  239. }
  240.  
  241.  
  242. inline int operator != (<T>List& x, <T>List& y)
  243. {
  244.   return !(x == y);
  245. }
  246.  
  247. inline Pix <T>List::first()
  248. {
  249.   return (P == &Nil<T>ListNode)? 0 : Pix(P);
  250. }
  251.  
  252. inline <T>& <T>List::operator () (Pix p)
  253. {
  254.   return ((<T>ListNode*)p)->hd;
  255. }
  256.  
  257. inline void <T>List::next(Pix& p)
  258. {
  259.   if (p != 0)
  260.   {
  261.     p = Pix(((<T>ListNode*)p)->tl);
  262.     if (p == &Nil<T>ListNode) p = 0;
  263.   }
  264. }
  265.  
  266. inline <T>List::<T>List(Pix p)
  267. {
  268.   P = (<T>ListNode*)p;
  269.   reference(P);
  270. }
  271.  
  272. #endif
  273.